<?php
require_once("conf/db_connection.php");
$trip = $_GET['trip'];
//$trucker = $_GET['trucker'];
$truck = $_GET['truck'];
$sms_to = $_GET['sms_to'];

$query_trucker = "SELECT trucker_id,contact_cellphone,plate FROM truck WHERE id='".$truck."'";
$result_trucker = mysql_query($query_trucker) or die("SELECT TRUCKER ID ".mysql_error());
$row_trucker = mysql_fetch_array($result_trucker);

$gen_tas = "";
$conf_prefix = "TAS";
$conf_zero = 6;
$num_zero = $conf_zero - strlen($trip);

while($num_zero>0)
{
	$zero .= '0';
	$num_zero--;
}
$gen_tas = $conf_prefix.$zero.$trip;

//$query_update = "UPDATE trip_detail SET tas_number='".$gen_tas."',trucker_id='".$row_trucker['trucker_id']."',truck_id='".$truck."',status='for confirmation' WHERE id='".$trip."'";
$query_update = "UPDATE trip_detail SET trucker_id='".$row_trucker['trucker_id']."',truck_id='".$truck."',status='for confirmation' WHERE id='".$trip."'";
mysql_query($query_update) or die("UPDATE TRIP DETAILS ".mysql_error());

$query_insert = "INSERT INTO status_log (trip_detail_id, status,updated_by) VALUES
('".$trip."','for confirmation','".$_SESSION["username_nn"]."')";
mysql_query($query_insert) or die("INSERT STATUS LOG".mysql_error());

$update_truck = "UPDATE truck SET is_available = '2' WHERE id = '".$truck."'";
mysql_query($update_truck) or die("UPDATE TRUCK AVAILABILITY ".mysql_error());

$query_avail = "INSERT INTO availability_log (truck_id, status) VALUES ('".$truck."','Assigned')";
mysql_query($query_avail) or die("INSERT AVAILABILITY LOG ".mysql_error());


$query_msg = "SELECT * FROM sms_template WHERE code='confirm'";
$result_msg = mysql_query($query_msg) or die("SELECT TEMPLATE ".mysql_error());
$row_msg = mysql_fetch_array($result_msg);



$query_trip = "SELECT * FROM trip_detail WHERE id='".$trip."'";
$result_trip = mysql_query($query_trip) or die("TRIP DETAIL");
$row_trip = mysql_fetch_array($result_trip);

$message_out = $row_msg['message'];
$message_out = str_replace("|plate|",$row_trucker['plate'],$message_out);
$message_out = str_replace("|tas|",$gen_tas,$message_out);

$message_out .= " Details: ".$row_trip['quantity']."x".$row_trip['container_size'];
$message_out .= " | ".$row_trip['customer'];
$message_out .= " | ".$row_trip['pick_address'];
$message_out .= " | ".$row_trip['contact_person'];
$message_out .= " | ".$row_trip['contact_number'];
$message_out .= " | ".$row_trip['instruction'];
//DEV 9 SIM - 09175858546
//$send_to = $row_trucker['contact_cellphone'];
//Sir Dennis - "09228783898";
$send_to = $sms_to;
//$send_to = "09052944288";
$arr_message = str_split($message_out,136);

$cnt = count($arr_message);
$ctr = 1;
foreach($arr_message as $mensahe)
{
	$query_sms = "INSERT INTO smsout (case_num, smsto, smsmsg, sent_by, devid)
	VALUES ('".$gen_tas."','".$send_to."','".$ctr.$cnt.$mensahe."','".$_SESSION["username_nn"]."','9')";
	mysql_query($query_sms) or die("SMS OUT FAILED ".mysql_error());
}

echo "1";
?>